package com.nbjtjc.safe.service.cost;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.cost.CostInjuryClaimMapper;
import com.nbjtjc.safe.mapper.cost.CostInjuryClaimRecordMapper;
import com.nbjtjc.safe.model.cost.CostInjuryClaim;
import com.nbjtjc.safe.model.cost.CostInjuryClaimRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CostInjuryClaimRecordService {
	@Autowired
	  private CostInjuryClaimRecordMapper costInjuryClaimRecordMapper;
	@Autowired
	  private CostInjuryClaimMapper costInjuryClaimMapper;
	
	  @Transactional(rollbackFor = {Throwable.class})
	  public Result add(CostInjuryClaimRecord costInjuryClaimRecord){
		        Result result=new Result();
		        if(ObjectUtil.isNull(costInjuryClaimRecord)
		        		||ObjectUtil.isNull(costInjuryClaimRecord.getInjuryClaimId())
		        		){		            
		        	result.setFalse("关联受伤索赔主表ID不能为空");
		            return result;
		        }
			    int claimId = costInjuryClaimRecord.getInjuryClaimId();
//主键为空或库中无此主键则为新建，插入数据
		        if(ObjectUtil.isNull(costInjuryClaimRecord.getId())|| ObjectUtil.isNull(costInjuryClaimRecordMapper.selectByPrimaryKey(costInjuryClaimRecord.getId()))){
		        	int res=costInjuryClaimRecordMapper.insertSelective(costInjuryClaimRecord);
			        if(res==0){
			            result.setFalse("插入受伤索赔记录失败");
			            return result;
			        }
				    res = costInjuryClaimMapper.updateTotalClaim(claimId);
				    if(res==0){
				    	result.setFalse("更新受伤索赔获赔总额失败");
					    return result;
				
				    }
		        }else
		        {
		        	int res=costInjuryClaimRecordMapper.updateByPrimaryKeySelective(costInjuryClaimRecord);
		        	 if(res==0){
		        		 result.setFalse("更新受伤索赔记录失败");
				         return result;

		        	 }
		        	res = costInjuryClaimMapper.updateTotalClaim(claimId);
				    if(res==0){
				    	result.setFalse("更新受伤索赔获赔总额失败");
					    return result;
				
				    }
		        }
		        result.setData(costInjuryClaimRecord);

		        return result;
		    }


	  @Transactional(rollbackFor = {Throwable.class})
	  public Result delete(int id){
	      Result result=new Result();
	      if(ObjectUtil.isNull(id)){
	          result.setFalse("删除对象不能为空");
	          return result;
	      }
	      CostInjuryClaimRecord costInjuryClaimRecord = costInjuryClaimRecordMapper.selectByPrimaryKey(id);
	      int claimId = costInjuryClaimRecord.getInjuryClaimId();
	      int res = costInjuryClaimRecordMapper.deleteByPrimaryKey(id);
	      if(res==0){
	    	  result.setFalse("删除失败");
		      return result;
	
	      }
	      res = costInjuryClaimMapper.updateTotalClaim(claimId);
	      if(res==0){
	    	  result.setFalse("更新受伤索赔获赔总额失败");
		      return result;
	
	      }
	      return result;
	  }
	  
	  @Transactional(rollbackFor = {Throwable.class})
	  public Result delete(CostInjuryClaimRecord costInjuryClaimRecord){
	      Result result=new Result();
	      int id=costInjuryClaimRecord.getId();
	      if(ObjectUtil.isNull(id)){
	          result.setFalse("作废对象不能为空");
	          return result;
	      }
	      
	      CostInjuryClaimRecord costRecord = costInjuryClaimRecordMapper.selectByPrimaryKey(id);
	      int claimId = costRecord.getInjuryClaimId();
	      
	      costInjuryClaimRecord.setCancel(1);
	      int res = costInjuryClaimRecordMapper.updateByPrimaryKeySelective(costInjuryClaimRecord);
	      if(res==0){
	    	  result.setFalse("作废失败");
		      return result;
	
	      }
	      res = costInjuryClaimMapper.updateTotalClaim(claimId);
	      if(res==0){
	    	  result.setFalse("更新受伤索赔获赔总额失败");
		      return result;
	
	      }
	      return result;
	  }

	  
	  public Result find(CostInjuryClaim costInjuryClaim) {
	      Result result = new Result();
	      PageHelper.startPage(costInjuryClaim.getPageCurrent(), costInjuryClaim.getPageSize());
	      //用索赔主表ID或年份查找索赔记录
	      //有索赔主表ID用ID，没有则用年份查出主表ID
	      //用索赔主表ID为条件查出记录
	      costInjuryClaim.setCancel(0);
	      List<CostInjuryClaim> costInjuryClaims= costInjuryClaimMapper.select(costInjuryClaim);
	      if(costInjuryClaims.size() != 1){
	    	  result.setFalse("条件查询受伤索赔结果为空或不唯一");
		      return result;
	      }
	      
	      List<CostInjuryClaimRecord> costInjuryClaimRecords = costInjuryClaimRecordMapper.selectByClaimId(costInjuryClaims.get(0).getId());
	      result.setData(new PageInfo<>(costInjuryClaimRecords));

	      return result;
	  }
	  

}
